// orderdetail.js
var app = getApp()
Page({
  data: {
    id:'',
    depth:1,
    orderlist:'',
    totalnumber:'',
    totalprice:'',
    transportFee:'',
    transportId:'',
    transportType:'',
    orderstatus:'',
    recipientName:'',
    recipientMobile:'',
    recipientProvince:'',
    recipientCity:'',
    recipientCounty:'',
    recipientAddress:'',
    orderid:'',
    updateDate:'',
    createDate:'',
    payDate:'',
    wedorphoto:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      depth: options.depth,
      image: app.globalData.image
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  onUnload: function () {
    //如果是用户放弃支付时，会跳转到该页面。
    //点击左上方的返回按钮时，返回到商品详情页（而不是商品确认页），所以需要返回两层。
    //这里先返回一层。这样就不用更改返回按钮的逻辑了
    // if (this.data.depth==2){
    //   wx.navigateBack({})
    // }
    
  },
  selectProduct(e){
    var id = e.currentTarget.dataset.id
    var type = e.currentTarget.dataset.type
    var url = ''
    if (type == '0') url ='../wedproductdetail/wedproductdetail?id='+id
    else url = '../photoproductdetail/photoproductdetail?id=' + id
    wx.redirectTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var host = app.globalData.host
    wx.request({
      url: host + "/order/get",
      data: {
        openId: wx.getStorageSync('openId'),
        id:that.data.id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var updateDate = new Date(res.data.data.updateDate).toLocaleString()
                .replace(/年/, '-').replace(/月/, '-').replace(/日/, '');
        var createDate = new Date(res.data.data.createDate).toLocaleString()
          .replace(/年/, '-').replace(/月/, '-').replace(/日/, '');
        var payDate = res.data.data.payDate
        if (payDate){
          payDate = new Date(res.data.data.payDate).toLocaleString()
            .replace(/年/, '-').replace(/月/, '-').replace(/日/, '');
        }

        that.setData({
          orderlist: res.data.data.orderSkuList,
          totalprice: res.data.data.currentPrice,
          totalnumber: res.data.data.orderSkuList.length,
          transportFee: res.data.data.transportFee,
          transportId: res.data.data.transportId,
          transportType: res.data.data.transportType,
          orderstatus: res.data.data.status,
          recipientName: res.data.data.recipientName,
          recipientMobile: res.data.data.recipientMobile,
          recipientProvince: res.data.data.recipientProvince,
          recipientCity: res.data.data.recipientCity,
          recipientCounty: res.data.data.recipientCounty,
          recipientAddress: res.data.data.recipientAddress,
          orderid: res.data.data.showCode,
          updateDate: updateDate,
          createDate: createDate,
          payDate: payDate
        })
        if (res.data.data.isPhoto == 1){
          that.setData({
            wedorphoto: false
          })
        }else{
          that.setData({
            wedorphoto: true
          })
        }
      }
    })
  },
  deleteOrder: function (e) {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '您确定要删除该订单吗？',
      success: function (res) {
        if (res.confirm) {
          var id = e.currentTarget.dataset.id
          var host = app.globalData.host
          wx.request({
            url: host + "/order/delete",
            data: {
              openId: wx.getStorageSync('openId'),
              id: id
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '删除成功，2秒后将跳转到订单页面',
                icon: 'success',
                duration: 2000
              })
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  topay: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var host = app.globalData.host
    wx.request({
      url: host + "/order/payByOrder",
      data: {
        openId: wx.getStorageSync('openId'),
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        var source = res.data.data
        wx.requestPayment({
          'timeStamp': source.timeStamp,
          'nonceStr': source.nonce_str,
          'package': source.package,
          'signType': source.signType,
          'paySign': source.paySign,
          'success': function (res) {
          },
          'fail': function (res) {
          }
        })
      }
    })
  }
})